package com.aplink.generic.xmlpullparser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.util.Xml;

import com.aplink.generic.pojo.MainMenu;
import com.aplink.generic.pojo.MainMenuItem;
import com.aplink.generic.xmlparser.XmlPaserUtil;

public class MainMenuXmlPullParser {
	public static String ACTIVITY_NAME = "activityName";
	public static String ICON = "icon";
	public static String ID = "id";
	public static String MAIN_MENU = "mainMenu";
	public static String MENU = "menu";
	private static String mNameSpace = null;
	public static String SUB_MENU = "subMenu";

	public static String TITLE = "title";

	public static MainMenu parse(int xmlFile, Context context) {
		InputStream inputStream = XmlPaserUtil.getXMLInputStream(xmlFile,
		        context);
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			parser.setInput(inputStream, null);
			parser.nextTag();
			return read(parser);
		} catch (XmlPullParserException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return new MainMenu();
	}

	private static MainMenu read(XmlPullParser pullParser)
	        throws XmlPullParserException, IOException {
		pullParser.require(XmlPullParser.START_TAG, mNameSpace, MAIN_MENU);
		MainMenu mainMenu = new MainMenu();
		while (pullParser.next() != XmlPullParser.END_TAG) {
			if (pullParser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}

			if (pullParser.getName().equalsIgnoreCase(MENU)) {
				readMainMenu(pullParser, mainMenu);
			}
		}
		return mainMenu;
	}

	private static void readMainMenu(XmlPullParser pullParser, MainMenu mainMenu)
	        throws XmlPullParserException, IOException {
		MainMenuItem menuItem = new MainMenuItem();
		pullParser.require(XmlPullParser.START_TAG, mNameSpace, MENU);
		// PARSE MENU ICON
		menuItem.setIcon(XmlPaserUtil.readAttribute(pullParser, mNameSpace,
		        ICON));
		// PARSE ACTIVITY NAME
		menuItem.setActivityName(XmlPaserUtil.readAttribute(pullParser,
		        mNameSpace, ACTIVITY_NAME));
		// PARSE TITLE
		menuItem.setTitle(XmlPaserUtil.readAttribute(pullParser, mNameSpace,
		        TITLE));
		// PARSE ID
		menuItem.setID(XmlPaserUtil.readAttribute(pullParser, mNameSpace, ID));
		mainMenu.getGroupItem().add(menuItem);

		ArrayList<MainMenuItem> menuItems = new ArrayList<>();
		while (pullParser.next() != XmlPullParser.END_TAG) {
			if (pullParser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			if (pullParser.getName().equalsIgnoreCase(SUB_MENU)) {
				MainMenuItem subMenuItem = new MainMenuItem();
				// PARSE MENU ICON
				subMenuItem.setIcon(XmlPaserUtil.readAttribute(pullParser,
				        mNameSpace, ICON));
				// PARSE ACTIVITY NAME
				subMenuItem.setActivityName(XmlPaserUtil.readAttribute(
				        pullParser, mNameSpace, ACTIVITY_NAME));
				// PARSE TITLE
				subMenuItem.setTitle(XmlPaserUtil.readAttribute(pullParser,
				        mNameSpace, TITLE));
				// PARSE ID
				subMenuItem.setID(XmlPaserUtil.readAttribute(pullParser,
				        mNameSpace, ID));
				menuItems.add(subMenuItem);
			}
			pullParser.nextTag();
		}
		if (!menuItems.isEmpty()) {
			mainMenu.getMainMenu().put(menuItem, menuItems);
		}
	}
}
